package com.idlecode.manage;

import cn.smallbun.screw.core.engine.EngineFileType;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.components.JBScrollPane;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-02-06 16:00
 **/
public class DocumentManage extends DialogWrapper {

    private ComboBox<EngineFileType> fileTypeComboBox;
    private JTextField versionTextField;
    private JTextArea descriptionTextArea;
    private Map<String, Object> condition;

    public DocumentManage(Map<String, Object> condition) {
        super(true);
        this.condition = condition;
        setTitle("数据库文档设置");
        init();
    }

    private static JPanel createPanelWithFixedSize(String labelText) {
        JLabel label = new JLabel(labelText);
        label.setPreferredSize(new Dimension(60, label.getPreferredSize().height));
        JPanel panel = new JPanel(new FlowLayout());
        panel.setPreferredSize(new Dimension(450, label.getPreferredSize().height));
        panel.add(label);
        return panel;
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setPreferredSize(new Dimension(450, 200));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 0, 5);

        // 设置布局约束
        gbc.weightx = 1.0;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weighty = 0.0; // 前两行小高度

        JPanel fileTypePane = createPanelWithFixedSize("文档类型:");
        fileTypeComboBox = new ComboBox<>(EngineFileType.values(), 346);
        fileTypePane.add(fileTypeComboBox);
        panel.add(fileTypePane, gbc);

        gbc.gridy = 1;
        JPanel versionPane = createPanelWithFixedSize("文档版本:");
        versionTextField = new JTextField("1.0.0");
        versionTextField.setColumns(31);
        versionPane.add(versionTextField);
        panel.add(versionPane, gbc);

        // 最后一行占据剩余空间
        gbc.gridy = 2;
        gbc.weighty = 1.0; // 剩余高度给最后一行
        gbc.fill = GridBagConstraints.BOTH; // 允许占据所有空余空间
        JPanel descriptionPane = createPanelWithFixedSize("文档描述:");
        descriptionTextArea = new JTextArea("数据库设计文档生成");
        descriptionTextArea.setLineWrap(true); // 自动换行
        descriptionTextArea.setWrapStyleWord(true); // 只在单词边界换行

        // 使用 JScrollPane 包裹 JTextArea
        JScrollPane scrollPane = new JBScrollPane(descriptionTextArea);
        scrollPane.setPreferredSize(new Dimension(341, 100)); // 设置 JScrollPane 的大小
        descriptionPane.add(scrollPane);
        panel.add(descriptionPane, gbc);

        return panel;
    }

    @Override
    protected void doOKAction() {
        saveConnectionSettings();
    }

    private void saveConnectionSettings() {
        EngineFileType fileType = (EngineFileType) fileTypeComboBox.getSelectedItem();
        if (fileType == null) {
            Messages.showMessageDialog("文件类型不能为空！", "警告", Messages.getWarningIcon());
            return;
        }
        condition.put("fileType", fileType);
        String version = versionTextField.getText();
        if (StringUtil.isEmpty(version)) {
            Messages.showMessageDialog("版本号不能为空！", "警告", Messages.getWarningIcon());
            return;
        }
        condition.put("version", version);

        String description = descriptionTextArea.getText();
        condition.put("description", description);
        super.doOKAction();
    }
}
